﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Cabal.Kernel.DataSet;
using Cabal.Kernel.Consts;
using System.Net;
using System.IO;

namespace Cabal.Kernel.Output
{
    public class BDPostMission : HttpRequestMission
    {
        public BDPostMission(BDPostArgs args)
            :base(args.GetRequest(),args.GetRequestData())
        {
            _args = args;
        }

        BDPostArgs _args;
        BDPostResult _result;

        //hiden members
        protected new HttpRequestResult DoRequest()
        {
            return base.DoRequest();
        }
        protected new HttpRequestResult GetRequestResult()
        {
            return base.GetRequestResult();
        }

        public BDPostResult PostRequest()
        {
            base.DoRequest(); //do request method can set running flag
            return GetPostResult();
        }
        public BDPostResult GetPostResult()
        {
            BDPostResult result;          // = new BDPostResult();
            HttpRequestResult httpresult; // = this.GetRequestResult();

            result = new BDPostResult();
            httpresult = GetRequestResult();

            if (result.weberror != null)
            {
                result.weberror = httpresult.error;
            }
            else
            {
                result.heads = httpresult.response.Headers;
                result.html = Encoding.Default.GetString(httpresult.data);
            }

            return result;
        }

        #region MissionData成员
        public override string GetMissionType()
        {
            return MissionData.MType_BaiduPost;
        }
        public override object GetResult()
        {
            return this.GetPostResult() as object;
        }
        public override object DoWork()
        {
            return this.PostRequest();
        }
        #endregion
    }
    public class BDPostArgs
    {
        public BDPostArgs(CabalSoul kernel)
        {
            _kernel = kernel;
        }

        CabalSoul _kernel;

        public BDPostType PostType;
        public string tn
        {
            get
            {
                if (PostType == BDPostType.发帖)
                    return _kernel.ConstControl.GetConst(ConstSupport.Const_BDPost_tn_Post);
                else
                    return _kernel.ConstControl.GetConst(ConstSupport.Const_BDPost_tn_Reply);
            }
        }

        public string BarName;      //word
        public string BarID;        //lm

        public string BS;           //bs
        public string VerifyCode;   //word1
        public string Str2;         //str2

        public string BaiduID;      //str3
        public string Str4;         //str4

        public string Tittle;       //ti
        public string Tittle1;      //ti1
        public string Article;      //co
        public string ImageLink;    //str1

        public string OtherCookies; //cookies
        public string Bduss;        //          cookies: bduss=[Bduss]; [OtherCookies]

        public string PostID;       //k
        public string ReplyCount;   //sc
        public string ReplyKey;     //rn

        public void Init(BDPostType type)
        {
            PostType = type;
        }
        public void SetBar(string name, string id)
        {
            BarName = name;
            BarID = id;
        }
        public void SetBS(string bsvalue, string verf, string str2)
        {
            BS = bsvalue;
            VerifyCode = verf;
            Str2 = str2;
        }
        public void SetBVP(string baiduid, string str4)
        {
            BaiduID = baiduid;
            Str4 = str4;
        }
        public void SetUser(string bduss,string othercookies)
        {
            Bduss = bduss;
            OtherCookies = othercookies;
        }
        public void SetArticle(string tittle, string article,string piclink)
        {
            Tittle = tittle;
            Article = article;
            ImageLink = piclink;
        }
        public void SetArticle(string tittle, string oldtittle, string article, string piclink)
        {
            Tittle = tittle;
            Tittle1 = oldtittle;
            Article = article;
            ImageLink = piclink;
        }

        public HttpWebRequest GetRequest()
        {
            HttpWebRequest request;

            string fmtCookie = _kernel.ConstControl.GetConst(ConstSupport.Const_BDPost_format_cookie);
            string fmtHead   = _kernel.ConstControl.GetConst(ConstSupport.Const_BDPost_format_HttpHeader);
            string uri       = _kernel.ConstControl.GetConst(ConstSupport.Const_BDPost_uri_post);
            string referer   = _kernel.ConstControl.GetConst(ConstSupport.Const_BDPost_head_Referer);

            string cookies   = string.Format(fmtCookie, BaiduID, Bduss, OtherCookies);
            string head      = string.Format(fmtHead, referer, cookies);

            request = HttpWebRequest.Create(uri) as HttpWebRequest;
            request.Headers.Add(head);

            return request;
        }
        public byte[] GetRequestData()
        {
            string fmtData = _kernel.ConstControl.GetConst(ConstSupport.Const_BDPost_format_postdata);
            string ct = _kernel.ConstControl.GetConst(ConstSupport.Const_BDPost_ct);

            string data = string.Format(fmtData,
                ct,
                _kernel.ConstControl.GetConst((PostType == BDPostType.发帖)?ConstSupport.Const_BDPost_tn_Post: ConstSupport.Const_BDPost_tn_Reply),
                BarName,
                BarID,
                PostID,
                ReplyCount,
                ReplyKey,
                BS,
                Str2,
                BaiduID,
                Str4,
                _kernel.ConstControl.GetConst(ConstSupport.Const_BDPost_head_Referer),
                Tittle,
                Tittle1,
                Article,
                ImageLink,
                VerifyCode);

            return Encoding.Default.GetBytes(data);
        }
    }
    public enum BDPostType
    {
        发帖,
        回帖,
    }
    public class BDPostResult
    {
        public Exception weberror;
        public string html;
        public WebHeaderCollection heads;
    }
}
